
import CryptoJS from 'crypto-js'
export const object2Str = (o : string|object): string => {
  return typeof o === 'string' ? o : JSON.stringify(o)
}

export const storage = {
  get:  (key: string) => {
    const value = sessionStorage.getItem(key);
    return JSON.parse(value)
  },

  set:  (key: string, value: any): void => {
    if(value !== null && value !== undefined) {
      sessionStorage.setItem(key, JSON.stringify(value))
    }
  },

  remove: (key: string): void => {
    sessionStorage.removeItem(key)
  },

  clear: (): void => {
    sessionStorage.clear()
  }
}

export const lStorage = {
  get:  (key: string) => {
    const value = localStorage.getItem(key);
    return JSON.parse(value)
  },

  set:  (key: string, value: any): void => {
    if(value !== null && value !== undefined) {
      localStorage.setItem(key, JSON.stringify(value))
    }
  },

  remove: (key: string): void => {
    localStorage.removeItem(key)
  },

  clear: (): void => {
    localStorage.clear()
  }
}


export function clone(obj:Object){
  return Object.create(Object.getPrototypeOf(obj), Object.getOwnPropertyDescriptors(obj));
}

export function uniqueArray(array:any, key:string){
  var result = [];
  for (var i = 0; i < array.length; i++) {
      var item = array[i];
      var repeat = false;
      for (var j = 0; j < result.length; j++) {
          if (item[key] == result[j][key]) {
              repeat = true;
              break;
          }
      }
      if (!repeat) {
          result.push(item);
      }
  }
  return result;
}

export function encrypt(word:string,keyStr:string){
  let key = CryptoJS.enc.Utf8.parse(keyStr);
  let srcs = CryptoJS.enc.Utf8.parse(word);
  let encrypted = CryptoJS.AES.encrypt(srcs, key, {mode:CryptoJS.mode.ECB,padding: CryptoJS.pad.Pkcs7});
  return encrypted.toString();
}

export function decrypt(word:string, keyStr:string){
  let key  = CryptoJS.enc.Utf8.parse(keyStr);
  let decrypt = CryptoJS.AES.decrypt(word, key, {mode:CryptoJS.mode.ECB,padding: CryptoJS.pad.Pkcs7});
  return CryptoJS.enc.Utf8.stringify(decrypt).toString();
}


/**
 * 深度遍历查找
 * @param {*} tree 树形数据
 * @param {*} target 想要查找的目标
 */
export function DFS (tree, target) {
  // 模拟栈，管理结点
  let stack = [tree]
  while (stack.length) {
    // 栈顶节点出栈
    let node = stack.pop()
    // 查找到目标，退出
    if (node.value === target) {
      return target
    }
    if (node.children && node.children.length) {
      // 将候选顶点入栈，进行下一次循环
      stack.push(...node.children.reverse())
    }
  }
}


/**
 * 查找题组下的所有题目
 * @param {*} tree 树形数据
 */
export function findQuestions (tree) {
  // 模拟栈，管理结点
  let stack = [tree]
  let res = []
  while (stack.length) {
    // 栈顶结点出栈
    let node = stack.pop()
    // 题组有题目
    if (node.question_list && node.question_list.length) {
      // 存储需要的数据
      res.push(...node.question_list)
    }
    // 题组下还有组
    if (node.sub_group_list && node.sub_group_list.length) {
      // 将候选顶点入栈，进行下一次循环
      stack.push(...node.sub_group_list)
    }
  }
  return res
}

